package com.it.you.common.utils;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * @Description: 日期校验工具类
 * @Author: gj
 * @Date: 2023/2/3
 */
public class DateValidUtil {

    /**
     * 例：2021-01-01 or 2021/01/01 or 20210101
     */
    public static final String DATE_REGEX = "([0-9]{4}-[0-9]{2}-[0-9]{2})|([0-9]{4}/[0-9]{2}/[0-9]{2})|([0-9]{4}[0-9]{2}[0-9]{2})";

    /**
     * 例：2021-01 or 2021/01 or 20210101
     */
    public static final String MONTH_REGEX = "([0-9]{4}-[0-9]{2})|([0-9]{4}/[0-9]{2}/)|([0-9]{4}[0-9]{2})";


    /**
     *利用正则表达式检查字符串是否为日期格式
     * @param date
     * @return
     */
    public static boolean isDate(String date){
        if(StringUtils.isBlank(date) || !date.matches(DATE_REGEX)){
            return false;
        }
        return true;
    }

    public static boolean isMonth(String date){
        if(StringUtils.isBlank(date) || !date.matches(MONTH_REGEX)){
            return false;
        }
        return true;
    }

    public static final String NORM_DATE_PATTERN = "([0-9]{4}-[0-9]{2}-[0-9]{2})";
    public static final String PURE_DATE_PATTERN= "([0-9]{4}[0-9]{2}[0-9]{2})";

    public static Date isSp(String date){
        if(StringUtils.isNotBlank(date) && date.matches(NORM_DATE_PATTERN)){
            return DateUtil.parse(date, DatePattern.NORM_DATE_PATTERN);
        }
        if(StringUtils.isNotBlank(date) && date.matches(PURE_DATE_PATTERN)){
            return DateUtil.parse(date, DatePattern.PURE_DATE_PATTERN);
        }
        return new Date();
    }

    public static void main(String[] args) {
        System.out.println(isSp("2023-02-01"));
    }

}
